package com.toman.ui.work.workshop;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Enum.NetworkEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.ChildCompleteCount;
import com.toman.JavaBean.CombineEntity;
import com.toman.JavaBean.PerentCompleteCount;
import com.toman.JavaBean.SendworkorderEntity;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.ProgressDialogUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.camera.Camera;
import com.toman.toolbar.ToolBarActivity;
import com.toman.widget.CustomProgressBar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class SubmitMoreInfActivity extends ToolBarActivity {

    @BindView(R.id.tv_completed_count4)
    TextView tvCompletedCount4;
    @BindView(R.id.tv_qualified_count4)
    TextView tvQualifiedCount4;
    @BindView(R.id.tv_scrapped_count4)
    TextView tvScrappedCount4;
    @BindView(R.id.tv_waste_count4)
    TextView tvWasteCount4;
    @BindView(R.id.tv_reuse_count4)
    TextView tvReuseCount4;
    @BindView(R.id.tv_remake_count4)
    TextView tvRemakeCount4;
    @BindView(R.id.rl_progress4)
    RelativeLayout rlProgress4;
    @BindView(R.id.tv_check_man)
    TextView tvCheckMan;
    @BindView(R.id.rl_check)
    RelativeLayout rlCheck;
    @BindView(R.id.tv_check_time)
    TextView tvCheckTime;
    @BindView(R.id.rl_check_time)
    RelativeLayout rlCheckTime;
    @BindView(R.id.ll_audit)
    LinearLayout llAudit;
    @BindView(R.id.rl_more)
    RelativeLayout rlMore;
    @BindView(R.id.tv_completed_count3)
    TextView tvCompletedCount3;
    @BindView(R.id.tv_qualified_count3)
    TextView tvQualifiedCount3;
    @BindView(R.id.tv_scrapped_count3)
    TextView tvScrappedCount3;
    @BindView(R.id.tv_waste_count3)
    TextView tvWasteCount3;
    @BindView(R.id.tv_reuse_count3)
    TextView tvReuseCount3;
    @BindView(R.id.tv_remake_count3)
    TextView tvRemakeCount3;
    @BindView(R.id.rl_progress3)
    RelativeLayout rlProgress3;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.tv_order_remark_name)
    TextView tvOrderRemarkName;
    @BindView(R.id.et_order_remark)
    EditText etOrderRemark;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.rl_more1)
    RelativeLayout rlMore1;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.pb_base)
    CustomProgressBar pbBase;
    @BindView(R.id.tv_completed_count)
    TextView tvCompletedCount;
    @BindView(R.id.tv_demand_count)
    TextView tvDemandCount;
    @BindView(R.id.tv_qualified_count)
    TextView tvQualifiedCount;
    @BindView(R.id.tv_waste_count)
    TextView tvWasteCount;
    @BindView(R.id.tv_scrapped_count)
    TextView tvScrappedCount;
    @BindView(R.id.tv_remake_count)
    TextView tvRemakeCount;
    @BindView(R.id.tv_reuse_count)
    TextView tvReuseCount;
    @BindView(R.id.rl_progress)
    RelativeLayout rlProgress;
    @BindView(R.id.tv_order_name)
    TextView tvOrderName;
    @BindView(R.id.tv_beltline_child)
    TextView tvBeltlineChild;
    @BindView(R.id.ll_more1)
    LinearLayout llMore1;
    @BindView(R.id.ll_1)
    LinearLayout ll1;
    @BindView(R.id.iv_more2)
    ImageView ivMore2;
    @BindView(R.id.rl_more2)
    RelativeLayout rlMore2;
    @BindView(R.id.tv_progress2)
    TextView tvProgress2;
    @BindView(R.id.pb_base2)
    CustomProgressBar pbBase2;
    @BindView(R.id.tv_completed_count2)
    TextView tvCompletedCount2;
    @BindView(R.id.tv_demand_count2)
    TextView tvDemandCount2;
    @BindView(R.id.tv_qualified_count2)
    TextView tvQualifiedCount2;
    @BindView(R.id.tv_waste_count2)
    TextView tvWasteCount2;
    @BindView(R.id.tv_scrapped_count2)
    TextView tvScrappedCount2;
    @BindView(R.id.tv_remake_count2)
    TextView tvRemakeCount2;
    @BindView(R.id.tv_reuse_count2)
    TextView tvReuseCount2;
    @BindView(R.id.rl_progress2)
    RelativeLayout rlProgress2;
    @BindView(R.id.tv_sendwork_code)
    TextView tvSendworkCode;
    @BindView(R.id.rl_order_code)
    RelativeLayout rlOrderCode;
    @BindView(R.id.tv_sendwork_date)
    TextView tvSendworkDate;
    @BindView(R.id.rl_order_num)
    RelativeLayout rlOrderNum;
    @BindView(R.id.tv_completework_date)
    TextView tvCompleteworkDate;
    @BindView(R.id.tv_plan_id)
    TextView tvPlanId;
    @BindView(R.id.tv_customer)
    TextView tvCustomer;
    @BindView(R.id.tv_artline)
    TextView tvArtline;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_batch_number)
    TextView tvBatchNumber;
    @BindView(R.id.tv_stove_number)
    TextView tvStoveNumber;
    @BindView(R.id.tv_sendwork_count)
    TextView tvSendworkCount;
    @BindView(R.id.ll_more2)
    LinearLayout llMore2;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    @BindView(R.id.tv_edit_man)
    TextView tvEditMan;
    @BindView(R.id.tv_edit_time)
    TextView tvEditTime;
    @BindView(R.id.ll_check)
    LinearLayout llCheck;
    @BindView(R.id.et_completed_count)
    EditText etCompletedCount;
    @BindView(R.id.et_qualified_count)
    EditText etQualifiedCount;
    @BindView(R.id.et_scrapped_count)
    EditText etScrappedCount;
    @BindView(R.id.et_waste_count)
    EditText etWasteCount;
    @BindView(R.id.et_remake_count)
    EditText etRemakeCount;
    @BindView(R.id.et_reuse_count)
    EditText etReuseCount;
    @BindView(R.id.ll_sumit_more_count)
    LinearLayout llSumitMoreCount;
    @BindView(R.id.tv_create_man)
    TextView tvCreateMan;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.ll_create)
    LinearLayout llCreate;
    @BindView(R.id.tv_shift)
    TextView tvShift;
    @BindView(R.id.rl_shift)
    RelativeLayout rlShift;
    @BindView(R.id.tv_operator)
    TextView tvOperator;
    @BindView(R.id.rl_operator)
    RelativeLayout rlOperator;
    @BindView(R.id.tv_operator_title)
    TextView tvOperatorTitle;
    private CombineEntity beanM;
    private String status;
    Dialog dialog;
    Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_more_inf);
        ButterKnife.bind(this);
        initToolBar("量产报工详情");
        initIntent();
        initView();
    }

    private void initView() {
        ViewUtil.unedit(etOrderRemark);
        etOrderRemark.setText(beanM.getReportAddDesc());
        String url[] = new String[beanM.getImages().size()];
        for (int i = 0; i < url.length; i++) {
            url[i] = beanM.getImages().get(i).getImagePath();

        }
        tvCreateMan.setText(beanM.getCreateUser().getRealName());
        tvCreateTime.setText(beanM.getCreateTime());
        tvOperator.setText(beanM.getOpUser().getRealName());
        tvShift.setText(beanM.getShiftEntity().getShiftName());
        ViewUtil.unclick(tvShift, tvOperator);
        PerentCompleteCount bean2 = beanM.getCombineParentSendworkorderEntity().getPerentCompleteCount();
        camera = ViewUtil.createPhoto("产品图片", url, camera, this, ll, true);
        ViewUtil.invisible(tvProgress, tvProgress2);
        tvQualifiedCount2.setText("合格数量:" + bean2.getQualifiedCount() + "");
        tvScrappedCount2.setText("报废数量:" + bean2.getScrappedCount() + "");
        tvCompletedCount2.setText(bean2.getCompletedCount() + "");
        tvWasteCount2.setText("料废数量:" + bean2.getWasteCount() + "");
        tvRemakeCount2.setText("返工数量:" + bean2.getRemakeCount() + "");
        tvReuseCount2.setText("回用数量:" + bean2.getReuseCount() + "");
        tvDemandCount2.setText(beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity().getSendworkCount() + "");


        pbBase2.setMaxProgress((Integer.parseInt(beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity().getSendworkCount() + "")));
        if (beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity().getSendworkCount() >= bean2.getCompletedCount()) {
            pbBase2.setCurProgress(Integer.parseInt(bean2.getCompletedCount() + ""), 2000);
        } else {
            pbBase2.setCurProgress(Integer.parseInt(beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity().getSendworkCount() + ""), 2000);
        }
        SendworkorderEntity sendworkorderEntity = beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity();

        tvSendworkDate.setText(sendworkorderEntity.getSendworkDate());
        tvCompleteworkDate.setText(sendworkorderEntity.getCompleteworkDate());
        tvSendworkCode.setText(sendworkorderEntity.getSendworkCode());
        tvPlanId.setText(beanM.getCombineParentSendworkorderEntity().getPlanEntity().getPlanNum() + "");
        tvCustomer.setText(beanM.getCombineParentSendworkorderEntity().getPlanEntity().getCustomer());
//       tvCustomer.setText();
        tvArtline.setText(beanM.getCombineParentSendworkorderEntity().getOperationArtlineEntity().getArtlineName());
        tvProductName.setText(beanM.getCombineParentSendworkorderEntity().getProductEntity().getProductName());
        tvBatchNumber.setText(sendworkorderEntity.getBatchNumber());
        tvStoveNumber.setText(sendworkorderEntity.getStoveNumber());
        tvSendworkCount.setText(beanM.getCombineParentSendworkorderEntity().getSendworkorderEntity().getSendworkCount() + "");

        ChildCompleteCount bean1 = beanM.getCombineChildSendworkMappingEntity().getChildCompleteCount();
        tvOrderName.setText(beanM.getCombineChildSendworkMappingEntity().getOperationorderEntity().getOrderName());
        tvBeltlineChild.setText(beanM.getCombineChildSendworkMappingEntity().getBeltlineEntity().getBeltlineName());
        tvQualifiedCount.setText("合格数量:" + bean1.getQualifiedCount() + "");
        tvScrappedCount.setText("报废数量:" + bean1.getScrappedCount() + "");
        tvCompletedCount.setText(bean1.getCompletedCount() + "");
        tvWasteCount.setText("料废数量:" + bean1.getWasteCount() + "");
        tvRemakeCount.setText("返工数量:" + bean1.getRemakeCount() + "");
        tvReuseCount.setText("回用数量:" + bean1.getReuseCount() + "");
        tvDemandCount.setText(beanM.getCombineChildSendworkMappingEntity().getSendworkOrderMappingEntity().getWorkCount() + "");
        pbBase.setMaxProgress((Integer.parseInt(beanM.getCombineChildSendworkMappingEntity().getSendworkOrderMappingEntity().getWorkCount() + "")));
        if (beanM.getCombineChildSendworkMappingEntity().getSendworkOrderMappingEntity().getWorkCount() >= bean1.getCompletedCount()) {
            pbBase.setCurProgress(Integer.parseInt(bean1.getCompletedCount() + ""), 2000);
        } else {
            pbBase.setCurProgress(Integer.parseInt(beanM.getCombineChildSendworkMappingEntity().getSendworkOrderMappingEntity().getWorkCount() + ""), 2000);
        }
        tvEditMan.setText(beanM.getUpdateUser().getRealName());
        tvEditTime.setText(beanM.getUpdateTime());
        if (status.equals("1")) {
            ViewUtil.visible(llAudit);
            tvCheckMan.setText(beanM.getAuditUser().getRealName());
            tvCheckTime.setText(beanM.getAuditTime());
        }

        tvQualifiedCount3.setText("合格数量:" + beanM.getCounts12().getQualifiedCount() + "");
        tvScrappedCount3.setText("报废数量:" + beanM.getCounts12().getScrappedCount() + "");
        tvCompletedCount3.setText("完工数量:" + beanM.getCounts12().getCompletedCount() + "");
        tvWasteCount3.setText("料废数量:" + beanM.getCounts12().getWasteCount() + "");
        tvRemakeCount3.setText("返工数量:" + beanM.getCounts12().getRemakeCount() + "");
        tvReuseCount3.setText("回用数量:" + beanM.getCounts12().getReuseCount() + "");
        Object[] str = new Object[]{beanM.getCounts12().getCompletedCount(), beanM.getCounts12().getQualifiedCount(), beanM.getCounts12().getRemakeCount(), beanM.getCounts12().getReuseCount(), beanM.getCounts12().getScrappedCount(), beanM.getCounts12().getWasteCount()};
        ViewUtil.setText(str, etCompletedCount, etQualifiedCount, etRemakeCount, etReuseCount, etScrappedCount, etWasteCount);

        tvQualifiedCount4.setText("合格数量:" + beanM.getCounts12().getVerifyQualifiedCount() + "");
        tvScrappedCount4.setText("报废数量:" + beanM.getCounts12().getVerifyScrappedCount() + "");
        tvCompletedCount4.setText("完工数量:" + beanM.getCounts12().getVerifyCompletedCount() + "");
        tvWasteCount4.setText("料废数量:" + beanM.getCounts12().getVerifyWasteCount() + "");
        tvRemakeCount4.setText("返工数量:" + beanM.getCounts12().getVerifyRemakeCount() + "");
        tvReuseCount4.setText("回用数量:" + beanM.getCounts12().getVerifyReuseCount() + "");


    }

    private void initIntent() {
        status = getIntent().getStringExtra("status");
        beanM = (CombineEntity) getIntent().getSerializableExtra("bean");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_edit, menu);


        MenuItem menuItem = menu.findItem(R.id.menu_edit);
        if (status.equals("1")) {
            menuItem.setVisible(false);
        }
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_edit:
                        if (item.getTitle().equals("编辑")) {
                            item.setTitle("完成");
                            ViewUtil.gone(rlProgress3);
                            ViewUtil.visible(llSumitMoreCount);
                            ViewUtil.edit(etOrderRemark);
                            ViewUtil.click(tvShift, tvOperator);
                            ViewUtil.necessary(tvOperator);
                            camera.init(false);
//                            修改数量状态

                        } else {
                            add2();
                        }
                        break;
                }
                return false;
            }
        });

        return true;
    }


    //    改
    public void add2() {
        String[] item2 = new String[]{"完工数量", "合格数量"};//直接初始化

        if ( ViewUtil.compareString(this,item2,etCompletedCount,etQualifiedCount)
            ) {
        return;
    }
        dialog = ProgressDialogUtils.showProgressDialog(this, "正在上传中", "请稍后");


        OkHttpUtils
                .post(NewBaseUrl.workshop_submit_benchreport_update)
                .headers("token", token)
                .headers("Content-Type", "multipart/form-data")
                .addFileParams("file", ViewUtil.getFiles(camera))
                .params("id", beanM.getId() + "")
                .params("batchReportAddDesc", etOrderRemark.getText().toString())
                .params("deleteUrls", ViewUtil.getDeleteUrl(camera))
                .params("completedCount", etCompletedCount.getText().toString())
                .params("qualifiedCount", etQualifiedCount.getText().toString())
                .params("scrappedCount", etScrappedCount.getText().toString())
                .params("wasteCount", etWasteCount.getText().toString())
                .params("remakeCount", etRemakeCount.getText().toString())
                .params("reuseCount", etReuseCount.getText().toString())
                .params("shiftId", beanM.getShiftId() + "")
                .params("opUserId", beanM.getOpUserId() + "")
                .execute(new StringCallback() {
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        Toast.makeText(getApplicationContext(), "请检查您的网络连接!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }


                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", getClass().toString() + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");
                        ViewUtil.lagout(code, getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }
                        dialog.dismiss();

                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        camera.onActivityResult(requestCode, resultCode, data);
    }


    @OnClick({R.id.rl_more1, R.id.rl_more2, R.id.tv_operator, R.id.tv_shift})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_more1:
                ViewUtil.hide(llMore1, ivMore);

                break;
            case R.id.rl_more2:
                ViewUtil.hide(llMore2, ivMore2);

                break;
            case R.id.tv_shift:
                Map<String, String> map = new HashMap<>();
                map.put("departmentId", departmentId);
                DialogUtils.showDialog(this, NetworkEnum.SHIFT, mCompanyId, map, new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        tvShift.setText(bean1.getName());
                        beanM.setShiftId(Long.valueOf(bean1.getID()));
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_operator:
                DialogUtils.showDialog(this, NetworkEnum.MAN, mCompanyId, new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        tvOperator.setText(bean1.getName());
                        beanM.setOpUserId(Long.valueOf(bean1.getID()));
                        dialog.dismiss();
                    }
                });
                break;
        }
    }
}
